<script>
import { selectPermissionList, addPermission, updatePermission, getPermissionDetail, deletePermission } from '@/api/permission'
import { transListToTreeData } from '@/utils'

export default {
  name: 'Permission',
  data() {
    return {
      title: '',
      showDialog: false,
      permissionList: [],
      formData: {
        name: '',
        code: '',
        type: null,
        pid: null,
        description: '',
        enVisible: 0
      },
      rules: {
        name: [
          { required: true, message: '权限名称不能为空', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '权限标识不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    // 获取权限列表
    async getPermissionList() {
      this.permissionList = transListToTreeData(await selectPermissionList(), 0)
    },
    // 添加权限打开弹窗
    openDialog(pid) {
      this.showDialog = true
      this.formData.pid = pid
      if (pid !== 0) {
        this.formData.type = 2
      } else {
        this.formData.type = 1
      }
    },
    // 查询权限详情
    async permissionDetail(id) {
      this.formData = await getPermissionDetail(id)
      this.showDialog = true
    },
    // 添加/修改权限
    submitPermission() {
      this.$refs.permissionRef.validate(async valid => {
        if (valid) {
          if (this.formData.id) {
            await updatePermission(this.formData)
            this.$message.success('修改权限成功')
          } else {
            // 新增权限点
            await addPermission(this.formData)
            this.$message.success('新增权限成功')
          }
          this.close()
          this.getPermissionList()
        }
      })
    },
    // 删除权限
    async deletePermission(id) {
      await deletePermission(id)
      this.$message.success('删除权限成功')
      this.getPermissionList()
    },
    // 关闭弹窗
    close() {
      this.showDialog = false
      this.formData = {
        name: '',
        code: '',
        type: null,
        pid: null,
        description: '',
        enVisible: 0
      }
      this.$refs.permissionRef.resetFields()
    }
  }
}
</script>

<template>
  <div class="container">
    <div class="app-container">
      <el-button class="add-button" size="mini" type="primary" @click="openDialog(0)">添加权限</el-button>
      <el-table
        default-expand-all
        row-key="id"
        :data="permissionList"
        :header-cell-style="{ background: '#f5f6f8' }"
        style="width: 100%;margin-top: 10px"
        border
        :tree-props="{children: 'children'}"
      >
        <el-table-column prop="name" align="left" label="名称" />
        <el-table-column prop="code" align="left" label="标识" />
        <el-table-column prop="description" align="left" label="描述" />
        <el-table-column align="left" label="操作">
          <template v-slot="{row}">
            <el-button v-if="row.type===1" type="text" @click="openDialog(row.id)">添加</el-button>
            <el-button type="text" @click="permissionDetail(row.id)">编辑</el-button>
            <el-button type="text" @click="deletePermission(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-dialog :title="formData.id ? '编辑权限点' : '新增权限点'" :visible="showDialog" width="50%" @close="close">
      <el-form ref="permissionRef" label-width="120px" :model="formData" :rules="rules">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" style="width: 90%" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" style="width: 90%" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="formData.description" style="width: 90%" />
        </el-form-item>
        <el-form-item label="开启" prop="enVisible">
          <el-switch v-model="formData.enVisible" active-value="1" inactive-value="0" />
        </el-form-item>
        <el-form-item>
          <el-row style="width: 90%">
            <el-col style="text-align: center">
              <el-button type="primary" size="small" @click="submitPermission">确 定</el-button>
              <el-button size="small" @click="close">取 消</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<style lang="scss" scoped>
.container {
  padding: 20px;
  .app-container {
    padding: 20px;
    background: #fff;
  }
}

</style>
